package javase.ch06.reflect.myPersonReflect;

import java.lang.reflect.Field;

public class Demo {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
        //1.获取Class字节码文件的对象
        Class clazz = Class.forName("reflect.myPersonReflect.Student");

        //2.获取所有得成员变量
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            System.out.println(field);
        }

        System.out.println("------------------------------------------------");
        //3.获取指定的成员变量
        Field name = clazz.getDeclaredField("name");
        System.out.println(name);

        //获取权限修饰符
        int modifiers = name.getModifiers();
        System.out.println(modifiers);

        //获取成员变量名
        String n = name.getName();
        System.out.println(n);

        //获取成员变量的类型
        Class<?> type = name.getType();
        System.out.println(type);

        //获取成员变量得值
        Student student = new Student("张三", 18, "男");
        name.setAccessible(true);
        Object value = name.get(student);
        System.out.println(value);

        //修改对象里成员变量的值
        name.set(student, "王五");
        System.out.println(student);
    }
}
